Computer languages utilize variables to manipulate, reference and represent data. For instance, in a conventional programming language a variable Temperature can be created, referencing a value stored in memory that represents the temperature of an underlying process being modeled the program. The computer can perform operations on the variable, changing the value referenced by Temperature. But the variable Temperature does not have an inherent understanding of the units for the data referenced therein.
In one example, the computer program simulates a process where the variable Temperature changes. The variable Temperature can be modified by the operation of the program. Temperature can be increased by addition of another value represented by the variable Increase. Temperature may represent a value stored in the Celsius scale, and Increase may represent a value stored in the Fahrenheit scale. If the computer program is not adapted correctly by the programmer to carefully monitor the units for each variable, the results from the operation of the program can be in error. In this example, if Increase is 5xc2x0 C. and Temperature is 0xc2x0 F., and the values are simply added, the result is 5 which, in either the Celsius or Fahrenheit scale, is incorrect. Without an understanding of the units represented by each variable, the program will operate incorrectly.
As another example, take a program calculating a force on an object. The variable Object-Mass can be assigned the mass of the object. Given that F=MA, an acceleration, here Acceleration, can be applied to the object, creating a force equal to Object-Mass*Acceleration. If this were assigned to a variable Force, again the programmer would be required to know the units in which Object-Mass and Acceleration were denominated so as to create the correct force unit.
In accordance with one aspect of the invention, an operation is performed on quantities in a data processing system. The quantities are stored in data structures, each data structure comprising a value and a unit designation representing a unit corresponding to the value. The operation is performed on the values from respective quantity data structures, and a corresponding operation is performed on the respective unit designations.
Operations on the unit designations can lead to errors, these errors can be detected at both compile-time and run-time. Operations, such as expression evaluation and assignment, may attempt to create units that are incompatible or overflowed. In one preferred embodiment, a method is performed at compile-time to check for an error condition in an operation on an operand quantity having unit designation. The method comprises: defining an operation on at least one operand, the one operand having a unit designation, the operation having a defined result having a unit designation, and performing, at compile-time, a unit error check on the operation.
In the preferred implementation, the unit designation represents exponents of a unit corresponding to the value. The unit designation may be a vector of exponents corresponding to physical units, preferably standard units of a measurement standard such as the SI standard. The unit designations may be checked for compatibility before an operation is performed. When the operation is multiplication, the exponents of respective unit designations are added. Where the operation is division, exponents of the respective units designations are subtracted. Where the operation is addition the resultant exponent is the same as the exponent of each of the unit designations. Where the operation is exponentiation, the exponents of the respective unit designations are multiplied.
The quantity data structure may further include a tag which provides a unit designation preferred for printing or as a definable units designation.
When a variable, including a unit string, is received and to be defined as a quantity, the unit designation may be obtained from the unit string as through a table lookup. Similarly, a conversion factor, either a conversion scale factor or a conversion algorithm, can be obtained from the unit string through a table lookup.
In a preferred embodiment, a variable structure is generated to store a received variable having a value and an associated unit designation. The variable structure has at least two storage slots, a first storage slot adapted to store the value and a second storage slot adapted to store a unit designation pointer. The unit designation of the received variable is stored in a unit designation structure and an address to the unit designation structure is stored in the second slot of the variable structure.
The unit designation may comprise one or more subunits, each having an exponent value, and the exponent values of the one or more subunits are stored. The one or more subunits may represent a basic unit representing a known physical quantity, preferably of the set consisting of distance, mass, time, electrical current, temperature, intensity of light, angle, and amount of substance. The one or more subunits may represent a user definable basic unit. A standard unit may be selected from a set of SI, mks, cgs and English systems of measurement.
In accordance with another aspect of the invention, an operation is performed on two variables on a computer implemented method. A first value is received from the first storage slot of a first of two variables and a second value is received from a first storage slot of a second of the two variables. The operation is performed on the first value and the second value creating a value result. A first unit designation is retrieved from the second storage slot of a first of the two variables and from the second storage slot of a second of the two variables. A unit designation result is representative of the results of the operation on the first unit designation and the second unit designation.
In accordance with a further aspect of the invention, the variable may have a standard unit designation or a definable unit designation. The variable structure has at least three storage slots. A first storage slot stores the value, a second storage slot stores the standard unit designation and the third storage slot stores the definable units designation.
In the preferred embodiment, both exactly defined quantities and underdefined quantities are stored as quantity data structures having values and unit designations.
The invention provides a system, method and data structure for handling special variables having both a value and a unit designation. Transparent support may be provided for manipulating such variables utilizing conventional numeric operators. Application of any such operations on the variables may result in an automatic check of the unit designations with a resulting error condition for improper usage. Thus, the system can create a variable having a distance unit designation and signal an error if the system attempts to add to it a variable having a time unit designation. In addition, if the operation were division, then this example would allow creation of a variable having a unit designation of distance per time.
Preferably, the system includes the ability to provide conversion factors to apply to variables to convert between unit types of the same kind. Thus, adding yards to meters automatically results in the correct answer.
Both a predetermined dictionary of standard units and a configurable dictionary of newly created units may be provided. The newly created units can further include conversion factors. Thus, an arbitrary unit designation can be created.
A preferred display mechanism allows the specification of a preferred presentation for viewing. Thus, a distance variable can include a xe2x80x9cyardsxe2x80x9d preferred presentation resulting in the conversion of the numeric value to the preferred units.
The invention can be implemented as a first class data type in a programming language. Variables created under the invention are thus treated equivalently as a conventional variable in the programming language.
Thus, it is an advantage of the invention to provide a system and method for manipulating units. Further advantages of the invention include the ability to provide a system and method for automatically and transparently associating a unit denomination with a variable and for coercing unit denominations during operations on variables. It is also an advantage of the invention to provide a system and method for converting unit denominations during operations on variables.
These and other additional advantages will become apparent in the detailed description of the preferred embodiments and associated drawings.